﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace ParkLBS.FrameWork
{
    public static class WebSiteDB
    {
        private static MyDBEntity s_db;

        public static MyDBEntity MyNorthwind
        {
            get
            {
                if (s_db == null)
                    throw new InvalidOperationException("DataBase is null.");

                return s_db;
            }
        }

        public static void LoadDbFromXml(string xmlPath)
        {
            if (System.IO.File.Exists(xmlPath) == false)
                throw new ArgumentException("指定的文件不存在：" + xmlPath);

            s_db = XmlHelper.XmlDeserializeFromFile<MyDBEntity>(xmlPath, Encoding.UTF8);
        }

        public static void SaveDbToXml(string xmlPath)
        {
            if (string.IsNullOrEmpty(xmlPath))
                throw new ArgumentNullException("xmlPath");

            if (s_db != null)
            {
                try
                {
                    XmlHelper.XmlSerializeToFile(s_db, xmlPath, Encoding.UTF8);
                }
                catch(Exception ex) { clsLogHelper.WriteLog(null, null, "更新xml出错"+ex.ToString()); }
            }
        }

    }
}
